<?php
	
	if($authInfo->usertype != '0') {
		redirectCleanly("/");
	}

	$tableWriter = new CmsTableWriter("grouplist",$db);
	$tableWriter2 = new CmsTableWriter("grouplist",$db);
	$error = false;
	
	$gid = extractStringWithKeyFromArray("gid", $_REQUEST);
	$groupname = extractStringWithKeyFromArray("groupname", $_REQUEST);
	$enablemode = extractStringWithKeyFromArray("enablemode", $_REQUEST);
	$enablesitemode = extractStringWithKeyFromArray("enablesitemode",$_REQUEST);
	$modecode = extractStringWithKeyFromArray("modecode", $_REQUEST);
	$modemask = extractStringWithKeyFromArray("modemask", $_REQUEST);
	$saveChanges = extractStringWithKeyFromArray("saveChanges", $_REQUEST);
	
	if ($gid) {
		$group = $tableWriter->readItem($gid);
		$isAdding = false;
		if (!$group) {
			$error = "<b>GROUP NOT FOUND</b>";
		}
	} else {
		$isAdding = true;
		$group = array();
	} 
	//group mode fangling
	{
		//biggest 
		$group->enablesitemode = ( ($group->enablemode & 2) >> 1);
		

		//this .. must always be last		.. to smallest
		$group->enablemode = ( $group->enablemode & 1 );

	}
	
	if ($saveChanges) {
		$db->executeQuery("SELECT * FROM grouplist WHERE groupname='".$groupname."'");
		if($db->numRows() > 0 && $isAdding) {
			$wname = "Group Name already exists!";
			$saveChanges = false;
		} else if (!$groupname) {
			$wname = "Please enter a name!";
			$saveChanges = false;
		}	
	}
	

?>
<div class="cms_browse_heading">
	<div class="cms_browse_heading_label">
		<h1>Browse Groups</h1>
	</div>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<div id="cms_browse">	
<script language="javascript" type="text/javascript">

function formSubmit() {	
	if (document.forms['groupForm'].groupname.value == "") {
		alert("Please enter a group name!");
		document.forms['groupForm'].groupname.select();
	}
	else {
		document.groupForm.submit();
	}
}
</script>
        <?
	if (!$error && !$saveChanges) {  
?> 		  
	<form action="index.php?fileNo=10" method="post" enctype="multipart/form-data" name="groupForm" id="groupForm">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="cms_browsetext">
      <tr>
        <td colspan="2"><img src="/cms/images/spacer.gif" width="100" height="5"></td>
      </tr>
      <tr>
        <td colspan="2"><img src="/cms/images/spacer.gif" width="450" height="5"></td>
      </tr>
	  <tr>
	    <td width="20%" >Group Name:</td>
	    <td ><input type="text" name="groupname" value="<?= $group->groupname ?>"></td>
	  </tr>
	  <tr>
	    <td width="20%" >Is Mode:</td>
	    <td >
	    <?
			echo jsDualStateCheckBox('enablemode',1,0,$group->enablemode);
	    ?>
	    </td>	  
	  </tr>
	  <tr>
	    <td width="20%" >Is Site:</td>
	    <td >
	    <?
			echo jsDualStateCheckBox('enablesitemode',1,0,$group->enablesitemode);
	    ?>
	    </td>	  
	  </tr>
	  <tr>
	    <td width="20%" >Mode Code:</td>
	    <td ><input type="text" name="modecode" value="<?= $group->modecode ?>"></td>
	  </tr>
	  <tr>
	    <td width="20%" >Mode Mask:</td>
	    <td ><input type="text" name="modemask" value="<?= $group->modemask ?>"></td>
	  </tr>	  
	  
	  <tr>
	    <td colspan="2">&nbsp;</td>
	  </tr>
	  <tr>
	    <td valign="middle"><!-- evil hack here for pages without a rich/wysiwig editor.. -->
	      <a href="javascript:formSubmit();" class="body2-2">Update/Add Now</a></td>
	    <td valign="middle"></td>
	  </tr>
    </table>
	<input type="hidden" name="saveChanges" value="saveChanges">
	<input type="hidden" name="gid" value="<?= $gid ?>">
	</form>
<?
	} else if (!$error && $saveChanges && !$gid){
		//echo "add";
			$tableWriter2->begin();
			$tableWriter2->addField("groupname",$groupname);
			$tableWriter2->addField("enablemode", $enablemode ^ ($enablesitemode << 1) );//join the modes
			$tableWriter2->addField("modecode", $modecode);
			$tableWriter2->addField("modemask", $modemask);
			$tableWriter2->newItem();	
			echo "<br><br><br><i>You have successfully added a new group.</i><br><br><br>";
		
	} else if (!$error && $saveChanges && $gid){
		//echo "edit";	
		$tableWriter2->begin();
		$tableWriter2->addField("groupname",$groupname);
		$tableWriter2->addField("enablemode", $enablemode ^ ($enablesitemode << 1));//join the modes
		$tableWriter2->addField("modecode", $modecode);
		$tableWriter2->addField("modemask", $modemask);
		$tableWriter2->addField("gid",$gid);
		$tableWriter2->saveItem();
		echo "<br><br><br><i>You have successfully edited the group.</i><br><br><br>";
	} else if ($error){
	dialoguePage(array(	
		DLG_MESSAGE=> 	$error,
		DLG_LEFT_TEXT=>	DLG_EMPTY,
		DLG_LEFT_LINK=>	CMS_HOME,
		DLG_RIGHT_TEXT=>	DLG_OK,
		DLG_RIGHT_LINK=>	CMS_HOME ));
	}	
?>
</table>
</div>